﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="shoppingcart.aspx.cs" Inherits="KTSonline.ajax.shoppingcart" %>

<%  if (Request.QueryString["empty"] != null)
    {
        if (Request.QueryString["empty"] == "1")
             Session["cart"] = null;
    }
    else if (Request.QueryString["increase"] != null)
    {
        String id = Request.QueryString["increase"];
        KTSonline.ShoppingCart cart = new KTSonline.ShoppingCart();
        if (Session["cart"] != null)
            cart = (KTSonline.ShoppingCart)Session["cart"];
        for (Int32 i = 0; i < cart.lstCart.Count; i++)
        {
            if (cart.lstCart[i].id == id)
            {
                cart.lstCart[i].quantity++;
                cart.quantity++;
                cart.totalprice += cart.lstCart[i].price;
                break;
            }
        }
    }
    else if (Request.QueryString["decrease"] != null)
    {
        String id = Request.QueryString["decrease"];
        KTSonline.ShoppingCart cart = new KTSonline.ShoppingCart();
        if (Session["cart"] != null)
            cart = (KTSonline.ShoppingCart)Session["cart"];
        for (Int32 i = 0; i < cart.lstCart.Count; i++)
        {
            if (cart.lstCart[i].id == id)
            {
                if (cart.lstCart[i].quantity == 1)
                    break;
                cart.lstCart[i].quantity--;
                cart.quantity--;
                cart.totalprice -= cart.lstCart[i].price;
                break;
            }
        }
    }
    else if (Request.QueryString["add"] != null)
    {
        String id = Request.QueryString["add"];
        KTSonline.ProductCart pd = new KTSonline.ProductCart(id);

        KTSonline.ShoppingCart cart = new KTSonline.ShoppingCart();
        if (Session["cart"] != null)
            cart = (KTSonline.ShoppingCart)Session["cart"];

        cart.AddProduct(pd);

        Session["cart"] = cart;
    }
    else if (Request.QueryString["delete"] != null)
    {
        String id = Request.QueryString["delete"];
        KTSonline.ShoppingCart cart = new KTSonline.ShoppingCart();
        if (Session["cart"] != null)
            cart = (KTSonline.ShoppingCart)Session["cart"];
        for (Int32 i = 0; i < cart.lstCart.Count; i++)
        {
            if (cart.lstCart[i].id == id)
            {
                cart.quantity -= cart.lstCart[i].quantity;
                cart.totalprice -= cart.lstCart[i].price * cart.lstCart[i].quantity;
                cart.lstCart.RemoveAt(i);
                break;
            }
        }
        if(cart.lstCart.Count == 0)
            Session["cart"] = null;
    }
    else if (Request.QueryString["update"] != null && Request.QueryString["newqty"] != null)
    {
        String id = Request.QueryString["update"];
        Int32 newqty = Int32.Parse(Request.QueryString["newqty"]);
        KTSonline.ShoppingCart cart = new KTSonline.ShoppingCart();
        if (Session["cart"] != null)
            cart = (KTSonline.ShoppingCart)Session["cart"];
        for (Int32 i = 0; i < cart.lstCart.Count; i++)
        {
            if (cart.lstCart[i].id == id)
            {
                if (cart.lstCart[i].quantity != newqty)
                {
                    cart.quantity = cart.quantity - cart.lstCart[i].quantity + newqty;
                    cart.totalprice = cart.totalprice - cart.lstCart[i].price * cart.lstCart[i].quantity + cart.lstCart[i].price * newqty;
                    cart.lstCart[i].quantity = newqty;
                }
                break;
            }
        }
    }
    if(Session["cart"] == null)
        Response.Write("<script type='text/javascript'>window.location.href='default.aspx'</script>"); 
    //else if (KTSonline.Global.currentpage == KTSonline.Global.Pages.CartDetail)
    //    Response.Write("<script type='text/javascript'>window.location.reload()</script>");     
%>

<div class="box_title">
    <% KTSonline.ShoppingCart scart = new KTSonline.ShoppingCart();
       String title = "Giỏ hàng (0)";
       if (Session["cart"] != null)
       {
           scart = (KTSonline.ShoppingCart)Session["cart"];
           title = "<a href='CartDetail.aspx'>Giỏ hàng (" + scart.quantity.ToString() + ")</a>";
           title += "<a style='float:right; padding-right:10px; font-size:90%; font-weight:normal;' href='Javascript:CartFunc(0, 2)'>Xóa hết</a>";
       }
       Response.Write(title);
    %>
    
</div>
<div class="box_content">
    <% if (scart.quantity > 0)
       {%>
    <table id="cart" cellspacing="0">
        <colgroup>
            <col class="col1" />
            <col class="col2" />
        </colgroup>
        <tbody>
            <tr>
                <th>Tên</th>
                <th>SL</th>
            </tr>
            <% foreach (KTSonline.ProductCart pd in scart.lstCart)
               {%>
            <tr>
                <td><%= pd.name %></td>
                <td><span style="margin-right:5px;" id="qty<%= pd.id %>"><%= pd.quantity %></span><img style="vertical-align:middle;" src="../images/updown.gif" alt="up" usemap="#updown<% Response.Write(pd.id); %>"/>
                    <map name="updown<%= pd.id %>">
                      <area shape="rect" coords="0,0,20,11" href="Javascript:CartFunc('<%= pd.id %>', 3)" alt="Add" title="Tăng" />
                      <area shape="rect" coords="0,12,20,23" href="Javascript:CartFunc('<%= pd.id %>', 4)" alt="Sub" title="Giảm"/>
                    </map>
                </td>
            </tr>
               <%}    
            %>
        </tbody>
    </table>
    <p class="total">Tổng cộng: <span class="sum"><%= scart.totalprice.ToString("N0") %> VNĐ </span></p>
    <%} %>
</div>